package com.javenwong.android.engine.biology;

import java.util.Vector;

import android.graphics.Canvas;

import com.javenwong.android.engine.GameCanvas;
import com.javenwong.android.engine.GameObject;
import com.javenwong.android.engine.scene.Camera;
import com.javenwong.android.engine.scene.Scene;

/**
 * Actor
 * 
 * @author 王晓锋
 * @version 1.00
 */
public class Actor extends GameObject {
	/** 玩家角色 */
	public static final byte TYPE_PLAYER_ROLE = -1;
	/** 怪物 */
	public static final byte TYPE_MONSTER = 0;
	/** NPC */
	public static final byte TYPE_NPC = 1;
	/** 地图动画 */
	public static final byte TYPE_MAP_ANIMATION = 2;
	/** BOSS */
	public static final byte TYPE_BOSS = 3;
	/** 触发区域 */
	public static final byte TYPE_TRIGGER = 4;
	/** 保存 */
	public static final byte TYPE_SAVE = 5;

	// 方向的定义
	public static final byte DIR_UP = 0;
	public static final byte DIR_DOWN = 1;
	public static final byte DIR_LEFT = 2;
	public static final byte DIR_RIGHT = 3;

	// 状态的定义
	/** 空闲 */
	public static final byte STATE_IDLE = 0;
	/** 行走 */
	public static final byte STATE_RUN = 1;

	/** 攻击 */
	public static final byte STATE_ATTACK = 2;
	/** 被攻击 */
	public static final byte STATE_PASSIVE = 3;
	/** 被击倒 */
	public static final byte STATE_DOWN_A_OUT = 7;
	/** 死亡 */
	public static final byte STATE_DEAD = 8;
	/** 技能1 */
	public static final byte STATE_SKILL_1 = 5;
	/** 技能2 */
	public static final byte STATE_SKILL_2 = 6;
	/** 胜利 */
	public static final byte STATE_SUCCESS = 4;
	/** 等待状态 */
	public static final byte STATE_WAIT = 10;
	public static final byte STATE_MOVE = 11;
	public static final byte STATE_BACK = 12;

	/** Actor ID */
	protected short id;

	/** Actor名称 */
	protected String name;

	/** 动画ID group_animation */
	protected String ani;

	/** 属性集 @see Actor.PARAM_ */
	protected Vector<String> params;

	/** Actor方向 */
	protected byte dir;

	/** Actor 状态 */
	protected byte state = STATE_IDLE;

	/** 当前动作是否已完成 */
	protected boolean actionOK = true;

	/** 4个方向的 站立、行走、攻击 动画 @see Actor.DIR_ */
	protected Animation[] directionAnimations = new Animation[32];

	public Animation animationCurrent;

	protected long frameTimeCount;

	/** Actor 当前移动步进 x */
	public int xSpeed = 0;
	/** Actor 当前移动步进 y */
	protected int ySpeed = 0;

	protected byte xMoveStep = 2;
	protected byte yMoveStep = 2;

	/** Actor 所在场景 */
	protected Scene scene;

	protected ActorAI ai;
	
	protected boolean visible = true;

	public Actor() {
	}

	public Actor(short id, int x, int y, String name, String ani,
			Vector<String> params) {

		super(id, x, y);

		this.ani = ani;
		this.params = params;
		this.name = name;
		// 加载动画

		if (ani != null && !ani.equals("")) {

			// 站立
			directionAnimations[0] = Animation.getAnimation(ani + "_" + 0);
			directionAnimations[1] = Animation.getAnimation(ani + "_" + 1);
			directionAnimations[2] = Animation.getAnimation(ani + "_" + 2);
			if (directionAnimations[2] != null) {
				directionAnimations[3] = Animation
						.getMirror(directionAnimations[2]);
			} else {
				directionAnimations[3] = Animation.getAnimation(ani + "_" + 3);
				if (directionAnimations[3]!=null) directionAnimations[2] = Animation
						.getMirror(directionAnimations[3]);
			}

			// 行走
			directionAnimations[4] = Animation.getAnimation(ani + "_" + 4);
			directionAnimations[5] = Animation.getAnimation(ani + "_" + 5);
			directionAnimations[6] = Animation.getAnimation(ani + "_" + 6);
			if (directionAnimations[6] != null) {
				directionAnimations[7] = Animation
						.getMirror(directionAnimations[6]);
			} else {
				directionAnimations[7] = Animation.getAnimation(ani + "_" + 7);
				if (directionAnimations[7]!=null) directionAnimations[6] = Animation
						.getMirror(directionAnimations[7]);
			}

			// 攻击
			directionAnimations[8] = Animation.getAnimation(ani + "_" + 8);
			directionAnimations[9] = Animation.getAnimation(ani + "_" + 9);
			directionAnimations[10] = Animation.getAnimation(ani + "_" + 10);
			if (directionAnimations[10] != null) {
				directionAnimations[11] = Animation
						.getMirror(directionAnimations[10]);
			} else {
				directionAnimations[11] = Animation
						.getAnimation(ani + "_" + 11);
				if (directionAnimations[11]!=null) directionAnimations[10] = Animation
						.getMirror(directionAnimations[11]);
			}

			// 初始动画
			dir = DIR_RIGHT;
			animationCurrent = directionAnimations[3];

		}
		// 初始化位置
		if (x == 0 && y == 0) {
			x = 100;
			y = 100;
		}
	}

	public void logic() {
		// 不断的动画
		// if (state==STATE_IDLE){
		animationCurrent.loopFrame(GameCanvas.timeCha);
		// }
		// else{
		// animationCurrent.nextFrame(timeCha);
		// }

		if (ai != null) {
			ai.logic(scene, this);
		}
	}

	public void draw(Canvas g, Camera camera) {

		if (animationCurrent != null) {
			animationCurrent.drawFrame(x - camera.getX(), y - camera.getY(), g);
		}
	}

	protected void move(int dx, int dy) {
		x += dx;
		y += dy;

	}

	protected void setPosition(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public Scene getScene() {
		return scene;
	}

	public void setScene(Scene scene) {
		this.scene = scene;
	}

	public ActorAI getAi() {
		return ai;
	}

	public void setAi(ActorAI ai) {
		this.ai = ai;
	}

	public void setDefaultAnimation(String ani) {

		int aniid = -1;

		try {
			aniid = Integer.valueOf(ani).intValue();
		} catch (NumberFormatException e) {
		}

		if (aniid != -1) {
			animationCurrent = directionAnimations[aniid];
		}
	}

	public Vector<String> getParams() {
		return params;
	}

	public void setParams(Vector<String> params) {
		this.params = params;
	}

	public byte getState() {
		return state;
	}

	public void setState(byte state) {
		this.state = state;
	}

	public int getxSpeed() {
		return xSpeed;
	}

	public void setxSpeed(int xSpeed) {
		this.xSpeed = xSpeed;
	}

	public int getySpeed() {
		return ySpeed;
	}

	public void setySpeed(int ySpeed) {
		this.ySpeed = ySpeed;
	}

	public byte getxMoveStep() {
		return xMoveStep;
	}

	public void setxMoveStep(byte xMoveStep) {
		this.xMoveStep = xMoveStep;
	}

	public byte getyMoveStep() {
		return yMoveStep;
	}

	public void setyMoveStep(byte yMoveStep) {
		this.yMoveStep = yMoveStep;
	}

	public short getId() {
		return id;
	}

	public void setId(short id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public byte getDir() {
		return dir;
	}

	public void setDir(byte dir) {
		this.dir = dir;
	}

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}

}
